package com.live.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.live.model.Admin;
import com.live.service.AdminService;


@WebServlet("/adminList")
public class AdminListController extends HttpServlet {
	private final Logger logger = Logger.getLogger(AdminListController.class);
	private static final long serialVersionUID = 1L;
	private static final int pageSize = 10;
	private AdminService adminService =null;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pageIndex = 1;
		if (null != request.getParameter("pageIndex") && Integer.parseInt(request.getParameter("pageIndex").trim())>0) {
			pageIndex = Integer.parseInt(request.getParameter("pageIndex").trim());
		}
		try {
		HttpSession session = request.getSession(true);
		Admin admin = (Admin) session.getAttribute("CurrentUser");
		if(admin !=null){
			adminService =new AdminService();
			List<Admin>  adminList = adminService.getAdminList(pageIndex, pageSize);
			request.setAttribute("adminList", adminList);// 传出adminList
			int totalCount =adminService.getTotalCount();
			request.setAttribute("totalCount", totalCount);
			int totalPage = adminService.getTotalPage(pageSize);
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("pageIndex", pageIndex);
			request.getRequestDispatcher("WEB-INF/view/adminList.jsp").forward(request,
				response);
		}else {
				request.getRequestDispatcher("login.jsp").forward(request,
					response);
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("login.jsp").forward(request,
					response);
			logger.debug("------执行员工列表doPost方法  执行员工列表doPost方法 = " + e.getMessage());
		}
	}
}